/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  XESConvertServiceImp.java                                      
 *  File Contents Outline :  日志转换服务
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.web.service.impl;

import cn.edu.thu.log.util.Timer;
import java.util.Collection;

import org.deckfour.xes.model.XLog;

import cn.edu.thu.log.web.service.WebConfigReadService;
import cn.edu.thu.log.web.service.XESConvertService;
import cn.edu.thu.log.xes.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.deckfour.xes.model.XTrace;


public class XESConvertServiceImp implements XESConvertService {
	XLog xlog;
	String readFilePath;
	String resultFile;
        private String classify;
         XESConvertor writer ;
	@Override
	public String getReadFilePath() {
		return readFilePath;
	}
	@Override
	public void setReadFilePath(String readFilePath) {
		this.readFilePath = readFilePath;
	}
	@Override
	public String getResultFile() {
		return resultFile;
	}
	@Override
	public void setResultFile(String resultFile) {
		this.resultFile = resultFile;
	}

	private static XESConvertService singleton = null;

	public synchronized static XESConvertService instance() {
		if (singleton == null) {
			singleton = new XESConvertServiceImp();
		}
		return singleton;
	}

	XESConvertServiceImp() {
//		readFilePath = new String();
//		resultFile = new String();

	}

	@Override
	public void convert() {
		if (readFilePath == null) {
			System.out.print("\nchoose log file");
		} else if (resultFile == null) {
			System.out.print("\ngive the name of resultFile");
		} else {
			WebConfigReadService configRead =  WebConfigReadServiceImpl.instance();
//			configRead.readWebConfig(PanGuConstants.CLEAN_CONFIGFILE);
			// String readFilePath = new
			// String("D:/imageclick_file/imageclick");
			// String resultFile = "logXes.xml";
			 writer = new XESConvertor(configRead, readFilePath,classify);
            try {
                writer.write(resultFile);
            } catch (Exception ex) {
                Logger.getLogger(XESConvertServiceImp.class.getName()).log(Level.SEVERE, null, ex);
            }
			xlog = writer.getLog();
		}
	}
//        @Override
//	public void convertWihtoutSplit() {
//		if (readFilePath == null) {
//			System.out.print("\nchoose log file");
//		} else if (resultFile == null) {
//			System.out.print("\ngive the name of resultFile");
//		} else {
//			WebConfigReadService configRead = WebConfigReadServiceImpl.instance();
////			configRead.readWebConfig(PanGuConstants.CLEAN_CONFIGFILE);
//			// String readFilePath = new
//			// String("D:/imageclick_file/imageclick");
//			// String resultFile = "logXes.xml";
//			XESConvertor writer = new XESConvertor(configRead, readFilePath);
//			writer.write(resultFile);
//			//xlog = writer.getLog();
//		}
//	}

	@Override
	public XLog getXlog() {

		return xlog;
	}

	@Override
	public Collection<XLog> readStandardLog(String standardLogFile) {
		XESReader xesReader = new XESReader();
		Collection<XLog> logs;
		logs = xesReader.readStandardLog(standardLogFile);
		return logs;
	}

	@Override
	public Collection<XLog> readStandardLog() {
		XESReader xesReader = new XESReader();
		Collection<XLog> logs;
		logs = xesReader.readStandardLog();
		return logs;
	}

	@Override
	public XLog mergeXESToLog(String sourcePath, String name) {
		//System.out.print("\nget into merge service");
		XESMerge xesMerge = new XESMerge();
		xlog = xesMerge.mergeXES(sourcePath, name);
		return xlog;
	}

          @Override
    public String getAnalysisResult() {
        int totalEvent = writer.getTotalEvent();
        Long totalTime =writer.getTimeTotal();
        Hashtable<String, Integer> caseBranchMap = writer.getCaseBranchMap();
      //  Hashtable<XTrace, String> earliestArrivalMap = XESConvertorMonitor.instance().getEarliestArrivalMap();
        Hashtable<XTrace, String> lastestArrivalMap = XESConvertorMonitor.instance().getLastestArrivalMap();
        String analysisResult = "";
        analysisResult = analysisResult + "the number of events:";
        analysisResult = analysisResult + totalEvent + "\n";
        analysisResult = analysisResult + "used time:";
        analysisResult = analysisResult + Timer.formatDuration(totalTime) + "\n";

        Enumeration<String> caseBranchInteror = caseBranchMap.keys();
        //  Enumeration<XTrace> xTraces=caseBranchMap.elements();
        int traceNum = 0;

        while (caseBranchInteror.hasMoreElements()) {
            String traceInfo = "";
            String caseBranch = caseBranchInteror.nextElement();
            traceInfo = traceInfo + "trace name:" + caseBranch + " ";
//            XTrace eachXTrace = caseBranchMap.get(caseBranch);
        //    String earliestTime = earliestArrivalMap.get(eachXTrace);
         //   traceInfo = traceInfo + "happens between time " + earliestTime;
        //    String latestTime = lastestArrivalMap.get(eachXTrace);
        //    traceInfo = traceInfo + "and " + latestTime;
            traceNum++;
           // analysisResult = analysisResult + traceInfo + "\n";
        }
        analysisResult = analysisResult + "the number of traces:";
        analysisResult = analysisResult + traceNum + "\n";
    //    System.out.println("\nanalysisResult:" + analysisResult);
        return analysisResult;

    }

    /**
     * @return the classify
     */
    @Override
	public String getClassify() {
        return classify;
    }

    /**
     * @param classify the classify to set
     */
    @Override
	public void setClassify(String classify) {
        this.classify = classify;
    }


}
